package com.tekj.cxqcb.view.Module.activity;

import android.Manifest;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qw.soul.permission.SoulPermission;
import com.qw.soul.permission.bean.Permission;
import com.qw.soul.permission.callbcak.CheckRequestPermissionListener;
import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.SelectMsgOfInviteBean;

import butterknife.BindView;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;
import common.plugins.tbs.WebCoreActivity;
import common.tool.Tools;
import es.dmoral.toasty.Toasty;

import static com.tekj.cxqcb.MainBActivity.User;
import static com.tekj.cxqcb.config.Urls.Domain_H5;

/**
 * 渠道拓展
 */
public class ExpandActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.tv_user_num)
    TextView tvUserNum;
    @BindView(R.id.tv_Cumulative)
    TextView tvCumulative;
    @BindView(R.id.tv_Record)
    TextView tvRecord;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_ljyq)
    TextView tvLjyq;

    BusinessDao businessDao;

    @Override
    public void init() {
        businessDao = new BusinessDao(activity, this);
        businessDao.SelectMsgOfInvite(User.getUserId(), 2);
    }

    @Override
    public int setLayout() {
        return R.layout.activity_expand;
    }

    @Override
    public void initLoadStart() {

    }

    @Override
    public void initLoadEnd() {

    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 拓展拉新人数:
                SelectMsgOfInviteBean inviteBean = (SelectMsgOfInviteBean) bindingViewBean.getBean();
                if (inviteBean.getCode().equals("0000000")) {
                    SetViewData(inviteBean.getData());
                }
                break;
        }
    }


    @OnClick({R.id.ll_back, R.id.tv_Record, R.id.tv_ljyq})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.tv_Record:

                startActivity(new Intent(activity, DevelopmentRecordActivity.class));

                break;
            case R.id.tv_ljyq:
                showPopLx("");
                break;
        }
    }

    void SetViewData(SelectMsgOfInviteBean.DataBean bean) {
        tvUserNum.setText("累计拓展渠道" + bean.getCount() + "位用户");
        tvCumulative.setText("累计获得奖励" + bean.getMoney() + "元");
    }

    private PopupWindow popupWindow;
    private View popupView;
    private TranslateAnimation animation;

    /**
     * 分享拉新
     */
    private void showPopLx(final String url) {
        Tools.lightoff(this);
        popupView = View.inflate(this, R.layout.pop_share_wx3, null);

        popupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT, true);
        popupWindow.setOutsideTouchable(true);

        animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,
                0, Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 1,
                Animation.RELATIVE_TO_PARENT, 0);
        animation.setInterpolator(new AccelerateInterpolator());
        animation.setDuration(200);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                Tools.lighton(activity);
            }
        });

        LinearLayout llWxHy = popupView.findViewById(R.id.ll_wx_hy);
        LinearLayout llWxPyq = popupView.findViewById(R.id.ll_wx_pyq);

//
        final CardView cv_bg = popupView.findViewById(R.id.cv_bg);
        ImageView ewm = popupView.findViewById(R.id.img_ewm);
        Tools.CreateQRcodeImage(
//                "https://cars-cxqc.com/static/league?referralCode="
                Domain_H5 + "/new-partner-recruit?referralCode=" + User.getMyCode(),
                ewm, activity, 90, 90);
        final CardView cv_wx = popupView.findViewById(R.id.cv_wx);

        cv_bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Tools.showAnimation(activity, cv_wx, cv_wx.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
            }
        });


        llWxHy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Tools.WXShareMini(activity, url,
////                        "/pages/partner-recruit/main?shareId=" + User.getUserId()
//                        "/pages/home/main?" + url + "&joinIn=true" + "&referralCode=" + User.getMyCode(),
//                        User.getName() + "邀请您立即加入...", "", R.drawable.icon_q);
                Tools.WXShareWeb(activity,
                        Domain_H5 + "/new-partner-recruit?referralCode=" + User.getMyCode(),
                        User.getNickname() + "邀请您立即加入...",
                        "超低利率、海量看车、一键选车、极速提车");
                popupWindow.dismiss();
            }
        });
        llWxPyq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SoulPermission.getInstance().checkAndRequestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        new CheckRequestPermissionListener() {
                            @Override
                            public void onPermissionOk(Permission permission) {
                                Tools.WxSharePhoto(cv_bg, activity);
                                popupWindow.dismiss();
                            }

                            @Override
                            public void onPermissionDenied(Permission permission) {
                                Toasty.normal(activity, "缺少存储权限！").show();
                            }
                        });

            }
        });


        popupWindow.showAtLocation(llBack, Gravity.BOTTOM, 0, 0);
        popupView.startAnimation(animation);

    }
}
